<?php
/*
---------------------------------------------------------
														+
名称:易通支付通道										+
														+
控制器名称:YiTongController								+
														+
日期:2017-10-11											+
														+
作者:小翁												+
														+
---------------------------------------------------------
*/
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;

class Juhewy2Controller extends PayController{
    protected $config;
    protected $key;
    protected $url;

    public function pay($array){
        $this->config = M("Channel")->where(array('id'=>$array['channel']))->find();
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>'14']);
        }
		
        $product = M("Product")->where(["id"=> I("request.pay_bankcode","")])->find();
        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>'014']);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        $data = array();

        //商户号
        $data['merchant_no'] = $this->config['mch_id'];
        //网银
        $data['merchant_channel_no'] = '3679ac59-be35-423c-a3a8-a521d9dd0f31';
        //交易金额
        $data['amount'] = $return['amount'];
        //业务订单号
        $data['busi_order_no'] = $return['orderid'];
        //业务用户号
        $data['busi_user_no'] = 'SHTTDZ';
        //业务商品号
        $data['busi_good_no'] = '100';
        //业务商品描述
        $data['busi_description'] = 'zf';
        //业务回传参数
        $data['busi_temp_info'] = 'zf';
        //IP
        //$data['client_ip'] = $_SERVER['REMOTE_ADDR'];
        //$data['client_ip'] = $_SERVER['REMOTE_ADDR'];
		$data['client_ip'] = $_SERVER['HTTP_X_FORWARDED_FOR'] ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR']; 

        $data['gateway_bank_code_flag'] = '';
        switch($product['code']){
            //北京银行
            case 'BOB':
                $data['gateway_bank_code_flag'] = '100200015';
                break;
            //东亚银行
            case 'BEA':
                $data['gateway_bank_code_flag'] = '';
                break;
            //中国工商银行
            case 'ICBC':
                $data['gateway_bank_code_flag'] = '100200002';
                break;
            //光大银行
            case 'CEB':
                $data['gateway_bank_code_flag'] = '100200010';
                break;
            //广发银行
            case 'CGB':
                $data['gateway_bank_code_flag'] = '100200013';
                break;
            //华夏银行
            case 'HXB':
                $data['gateway_bank_code_flag'] = '100200008';
                break;
            //中国建设银行
            case 'CCB':
                $data['gateway_bank_code_flag'] = '100200003';
                break;
            //交通银行
            case 'BOCOM':
                $data['gateway_bank_code_flag'] = '100200004';
                break;
            //中国民生银行
            case 'CMBC':
                $data['gateway_bank_code_flag'] = '100200007';
                break;
            //南京银行
            case 'NJCB':
                $data['gateway_bank_code_flag'] = '';
                break;
            //宁波银行
            case 'NBCB':
                $data['gateway_bank_code_flag'] = '';
                break;
            //中国农业银行
            case 'ABC':
                $data['gateway_bank_code_flag'] = '100200001';
                break;
            //平安银行
            case 'PAB':
                $data['gateway_bank_code_flag'] = '100200014';
                break;
            //上海银行
            case 'SHB':
                $data['gateway_bank_code_flag'] = '100200016';
                break;
            //上海浦东发展银行
            case 'SPDB':
                $data['gateway_bank_code_flag'] = '100200012';
                break;
            //深圳发展银行
            case 'SDB':
                $this->showmessage('暂不支持此银行卡!',['code'=>'012']);
                break;
            //兴业银行
            case 'CIB':
                $data['gateway_bank_code_flag'] = '100200009';
                break;
            //中国邮政储蓄银行
            case 'PSBS':
                $data['gateway_bank_code_flag'] = '100200006';
                break;
            //招商银行
            case 'CMB':
                $data['gateway_bank_code_flag'] = '100200005';
                break;
            //浙商银行
            case 'CZB':
                $data['gateway_bank_code_flag'] = '';
                break;
            //中国银行
            case 'BOC':
                $data['gateway_bank_code_flag'] = '100200000';
                break;
            //中信银行
            case 'CTTIC':
                $data['gateway_bank_code_flag'] = '100200011';
                break;
            default:
                $this->showmessage('暂不支持此银行卡!',['code'=>'012']);
        }
        if(!$data['gateway_bank_code_flag']){
            $this->showmessage('暂不支持此银行卡!',['code'=>'012']);
        }

        //回调地址
        $data['notify_url'] = $return['notifyurl'];
        //同步返回地址
        $data['return_url'] = $return['callbackurl'];

        $str = $this->config['signkey'] . $data['merchant_no'] . $data['amount'] . $data['merchant_channel_no'] . $data['notify_url'] . $data['client_ip'] . $data['busi_user_no'] . $data['busi_order_no'] . $data['busi_good_no'] . $data['busi_description'] . $data['gateway_bank_code_flag'];
        $data['sign'] = md5($str);

        M("Order")->where([ 'pay_orderid'=>$return['orderid']])->save(["request"=>json_encode($data)]);

        //echo '<pre/>'; print_r($data); exit;
        po_get($this->config['gateway'], $data);
    }

    //通知
    public function notifyurl(){
        //file_put_contents("a.txt",var_export($_POST,true)."\r\n\r\n");
        //file_put_contents("a.txt",var_export($_GET,true."\r\n\r\n"), FILE_APPEND);
        //file_put_contents("a.txt", file_get_contents('php://input')."\r\n\r\n", FILE_APPEND);
        //file_put_contents("a.txt", date('Y-m-d H:i:s',time())."\r\n\r\n", FILE_APPEND);

        $s = file_get_contents('php://input');

        //$s = '{"trans_no":"f0ac6fb6212747e6b2ddc1c0e6690b67","amount":"10.0000","busi_user_no":"SHTTDZ","busi_order_no":"201808171457061595515","busi_good_no":"100","busi_description":"zf","busi_temp_info":"zf","trans_flag":100150001,"pay_time":1534489069780,"sign":"062bbe9da801784cc5c1d88ede5e82fa"}';
        $p = $data = json_decode($s, true);
        if(!$p || empty($p)){
            exit;
        }
        //获取订单信息
        $orderid = $p['busi_order_no'];
        $find = M("Order")->where(array("pay_orderid"=>$orderid))->find();

        if(!$find){
            echo 'order_not_found'; exit;
        }

        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$p['busi_order_no']))->save(array("server_return"=> $s,"pay_orderid_rd"=>$p['trans_no']));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>'Juhewy2'))->find();

        if($p['trans_flag'] == 100150002 || $p['trans_flag'] == 100150000){
            echo "pail_fail"; exit;
        }

        $request = json_decode($find['request'], true);

        $sign = $p['sign'];

        $str = $this->config['signkey'].$request['merchant_no'].$p['amount'].$request['merchant_channel_no'].$request['notify_url'].$request['client_ip'].$p['busi_user_no'].$p['busi_order_no'].$p['busi_good_no'].$p['busi_description'];
        $sign2 = md5($str);


        //echo '<pre/>'; print_r($p); print_r($request);

        //echo 'sign: '.$sign.'<br/>';
        //echo 'sign2: '.$sign2;exit;

        if($sign != $sign2){
            exit("fail");
        }

        //处理支付
        $this->EditMoney($find['pay_orderid'],"Juhewy2",0);
        //回调成功
        echo 'SUCCESS'; exit;
    }

    public function callBackUrl(){

        echo '支付成功'; exit;
    }
}